<template>
  <div class="app-container">
    <div class="filter-container">
      分类名称:
      <el-input v-model="listQuery.name" label="分类名称" placeholder="关键词" style="width: 200px;"
        class="filter-item mring10" @keyup.enter.native="handleFilter" suffix-icon="el-icon-search" />
      <el-button v-waves class="filter-item mring10" type="primary" icon="el-icon-search" @click="handleFilter"
        size="small"> 搜索</el-button>
      <el-button v-waves class="right" type="success" icon="el-icon-plus" @click="handleCreate()" size="small"> 添加分类
      </el-button>
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" fit stripe highlight-current-row
      style="width: 100%;margin-top: 1.125rem" @sort-change="sortChange" row-key="id"
      :tree-props="{ children: 'children' }">
      >
      <!-- <el-table-column label="编号" prop="id" sortable="custom" align="center" width="80" :class-name="getSortClass('id')"></el-table-column> -->
      <el-table-column label="分类名字" prop="title" min-width="150"></el-table-column>
      <el-table-column label="排序" prop="sort" width="80"></el-table-column>
      <el-table-column label="状态" prop="status_name" class-name="status-col" width="100"></el-table-column>
      <el-table-column label="创建时间" prop="create_time" width="150" align="center">
        <template slot-scope="{row,$index}">
          {{ row.create_time | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <span class="btn-blue" @click="handleUpdate(row)">编 辑</span><i>|</i> <span @click="handleDelete(row, $index)"
            class="btn-blue">删 除</span>
        </template>
      </el-table-column>
      <!-- <template slot="empty" >
        <span style="font-size: 20px">会员可以点击'选择产品'进行数据查看</span>
      </template> -->
    </el-table>

    <pagination hide-on-single-page v-show="total > 0" :total="total" :page.sync="listQuery.page"
      :limit.sync="listQuery.size" @pagination="getList" />
  </div>
</template>

<script>
import { fetchList, selectTree, updateHelpCategory, createHelpCategory, deleteHelpCategory } from '@/api/operations/helpCategory'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    typeFilter(type) {
      //return calendarTypeKeyValue[type]
    }
  },
  data() {
    return {
      default_select: [0],
      tableKey: 0,
      list: [],
      total: 0,
      pid: 0,
      cateTree: [],
      listLoading: true,
      listQuery: {
        //page: 1,
        //size: 20,
        title: undefined,
        //sort: '+id'
      },
      temp: { name: '', pid: 0, status: 1, image: '' },
      dialogFormVisible: false,
      dialogImageVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑分类',
        create: '新增分类'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        name: [{ required: true, message: '分类名字不能为空', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data
        this.list.map(v => {
          v.status_name = v.status == 1 ? '启用' : '禁用';

          if (v.children && v.children.length > 0) {
            v.children.map(vv => {
              vv.status_name = vv.status == 1 ? '启用' : '禁用';
              if (vv.children && vv.children.length > 0) {
                vv.children.map(vvv => {
                  vvv.status_name = vvv.status == 1 ? '启用' : '禁用';
                })
              }
            })
          }
          // v.create_time = Date.getDate(v.create_time)
        })
        this.total = response.data.length

        // Just to simulate the time of the request
        this.listLoading = false
      })
    },
    updateData() {
      this.listLoading = true
      updateHelpCategory(this.temp).then(response => {
        if (response.code == 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
        }
        this.dialogFormVisible = false
        this.listLoading = false
        this.getList()
      })

    },
    createData() {
      this.listLoading = true
      createHelpCategory(this.temp).then(response => {
        if (response.code == 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
        }
        this.dialogFormVisible = false
        this.listLoading = false
        this.getList()
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作Success',
        type: 'success'
      })
      row.status = status
    },
    getTree() {
      if (this.cateTree.length < 1) {
        selectTree().then(response => {
          this.cateTree = response.data
        })
      }
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
    resetTemp() {
      this.temp = {
        name: '',
        pid: 0,
        status: 1,
        image: ''
      }
    },
    handleCreate() {
      this.$router.push({ name: 'createHelpCategory' })
    },
    handleUpdate(row) {
      this.$router.push({ name: 'createHelpCategory', params: { id: row.id } })
    },
    handleDelete(row, index) {
      deleteHelpCategory(row).then(response => {
        if (response.code == 200) {
          this.$notify({
            title: 'Success',
            message: '删除成功!',
            type: 'success',
            duration: 2000
          })

          this.list.splice(index, 1)
        }
      })
    },
    handleFetchPv(pv) {
      fetchPv(pv).then(response => {
        this.pvData = response.data.pvData
        this.dialogPvVisible = true
      })
    },

    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },
    getSortClass: function (key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    },

  }
}
</script>
<style>
.filter-container {
  background: #FFFFFF;
  padding: 0.625rem 1.125rem;
}

.btn-blue {
  color: #1890ff;
  padding: 0 0.625rem;
  cursor: pointer;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.filter-item input[type='text'] {
  height: 32px;
  line-height: 32px;
  font-size: 12px;
}

.mring10 {
  margin-right: 10px;
}
</style>
